package edu.hubu.talentmanagementsystem.auth;

import edu.hubu.talentmanagementsystem.common.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author moonlan
 * date 2021/3/19 下午4:50
 */
@SuppressWarnings(value = "unused")
public class SecurityAuthenticationEntryPoint implements AuthenticationEntryPoint {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
        response.setStatus(401);
        logger.error(authException.getMessage());
        CommonResult.write(request, response, new RuntimeException("Authentication Failed"));
    }
}
